package com.caipin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipin.dao.ClassroomDao;
import com.caipin.domain.table.Classroom;
import com.caipin.domain.web.PageBean;
import com.caipin.service.ClassroomService;
import com.caipin.service.ClbumManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-07-29 15:46
 * @Description: TODO
 */
@Service
public class ClbumManagementServiceImpl implements ClbumManagementService {

    @Autowired
    private ClassroomDao classroomDao;

    @Autowired
    private ClassroomService classroomIService;

    @Override
    @Transactional
    public int importClassroomToMysql(List<Classroom> classroomList) {
        classroomDao.truncateData();

        boolean b = classroomIService.saveBatch(classroomList);
        if (!b) {
            return 0;
        }

        return classroomList.size();

    }

    @Override
    public PageBean<Classroom> findClassroomList(Integer pageNum, Integer pageSize) {
        Page<Classroom> classroomPage = new Page<>(pageNum, pageSize);
        IPage<Classroom> page = classroomDao.selectPage(classroomPage, null);

        PageBean<Classroom> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());


        return pageBean;
    }

    @Override
    public PageBean<Classroom> findClassroomList(Integer pageNum, Integer pageSize, String buildingName, String classroomId, String classroomName, String grade, String clbum, String teacherId, String teacherName) {

        Page<Classroom> classroomPage = new Page<>(pageNum, pageSize);

        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        if (buildingName != null) {
            queryWrapper.eq("building_name", buildingName);
        }

        if (classroomId != null) {
            queryWrapper.eq("classroom_id", classroomId);
        }

        if (classroomName != null) {
            queryWrapper.eq("classroom_name", classroomName);
        }

        if (grade != null) {
            queryWrapper.eq("grade", grade);
        }

        if (clbum != null) {
            queryWrapper.eq("clbum", clbum);
        }

        if (teacherId != null) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (teacherName != null) {
            queryWrapper.eq("teacher_name", teacherName);
        }
        IPage<Classroom> page = classroomDao.selectPage(classroomPage, queryWrapper);
        PageBean<Classroom> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());


        return pageBean;
    }

    @Override
    public Classroom findClassroomById(Integer id) {
        return classroomDao.selectById(id);
    }

    @Override
    public void delClassroomById(Integer id) {
        classroomDao.deleteById(id);
    }

    @Override
    public int delClassroomListByIds(List<Integer> ids) {
        int i = classroomDao.deleteBatchIds(ids);
        return i;
    }

    @Override
    public void addClassroom(Classroom classroom) {
        classroomDao.insert(classroom);
    }

    @Override
    public void updateClassroom(Classroom classroom) {
        classroomDao.updateById(classroom);
    }
}
